package com.llkj.qingnianguoxue.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.llkj.qingnianguoxue.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * 分享
 */
public class MyShare {
    public static void share(final Context context, int shareType, String title, String text, String imgUrl, String linkUrl, final ShareResultCallback callBack) {
        switch (shareType) {
            case 1:
                //微信分享
                Wechat.ShareParams sp = new Wechat.ShareParams();
                sp.setTitle(title);
                sp.setText(text);
                if(!StringUtils.isEmpty(imgUrl)) {
                    sp.setImageUrl(imgUrl);
                }else{
                    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_invite_friend);
                    sp.setImageData(bitmap);
                }
                sp.setUrl(linkUrl);
                sp.setShareType(Wechat.SHARE_WEBPAGE);
                Platform wechats = ShareSDK.getPlatform(Wechat.NAME);
                wechats.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        callBack.shareResultCallBack(1);
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        callBack.shareResultCallBack(2);
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        callBack.shareResultCallBack(3);
                    }
                });
                wechats.share(sp);
                break;
            case 2:
                //朋友圈分享
                WechatMoments.ShareParams wsp = new WechatMoments.ShareParams();
                wsp.setTitle(title);
                wsp.setText(text);
                if(!StringUtils.isEmpty(imgUrl)) {
                    wsp.setImageUrl(imgUrl);
                }
                wsp.setUrl(linkUrl);
                wsp.setShareType(Wechat.SHARE_WEBPAGE);
                Platform wechat = ShareSDK.getPlatform(WechatMoments.NAME);
                wechat.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        callBack.shareResultCallBack(1);
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        callBack.shareResultCallBack(2);
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        callBack.shareResultCallBack(3);
                    }
                });
                wechat.share(wsp);
                break;
            case 3:
                //QQ分享
                QQ.ShareParams ssp = new QQ.ShareParams();
                ssp.setTitle(title);
                ssp.setText(text);
                if(!StringUtils.isEmpty(imgUrl)){
                    ssp.setImageUrl(imgUrl);
                }
                ssp.setTitleUrl(linkUrl);
                Platform weibo = ShareSDK.getPlatform(QQ.NAME);
                // 设置分享事件回调
                weibo.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        ToastUtil.makeLongText(context,"分享成功");
                        callBack.shareResultCallBack(1);
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        ToastUtil.makeLongText(context,"分享失败");
                        callBack.shareResultCallBack(2);
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        ToastUtil.makeLongText(context,"取消分享");
                        callBack.shareResultCallBack(3);
                    }
                });
//                weibo.SSOSetting(true);
                weibo.share(ssp);
                break;
            case 4://sina分享
                SinaWeibo.ShareParams sina = new SinaWeibo.ShareParams();
                sina.setTitle(title);
                sina.setText(text);
                if(!StringUtils.isEmpty(imgUrl)) {
                    sina.setImageUrl(imgUrl);
                }
                sina.setUrl(linkUrl);
                Platform sinaweibo = ShareSDK.getPlatform(SinaWeibo.NAME);
                // 设置分享事件回调
                sinaweibo.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        ToastUtil.makeLongText(context,"分享成功");
                        callBack.shareResultCallBack(1);
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        ToastUtil.makeLongText(context,"分享失败");
                        callBack.shareResultCallBack(2);
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        ToastUtil.makeLongText(context,"取消分享");
                        callBack.shareResultCallBack(3);
                    }
                });
                sinaweibo.SSOSetting(true);
                sinaweibo.share(sina);
                break;
            case 5://qq空间分享
                QZone.ShareParams qZone = new QZone.ShareParams();
//                qZone.setShareType(QZone.SHARE_TEXT);
                qZone.setTitle(title);
                qZone.setTitleUrl(linkUrl);
                qZone.setText(text);
                if(!StringUtils.isEmpty(imgUrl)) {
                    qZone.setImagePath(imgUrl);
                }
//                qZone.setSiteUrl(linkUrl);
                Platform qZonePlat = ShareSDK.getPlatform(QZone.NAME);
                // 设置分享事件回调
                qZonePlat.setPlatformActionListener(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        ToastUtil.makeLongText(context,"分享成功");
                        callBack.shareResultCallBack(1);
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        ToastUtil.makeLongText(context,"分享失败");
                        callBack.shareResultCallBack(2);
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        ToastUtil.makeLongText(context,"取消分享");
                        callBack.shareResultCallBack(3);
                    }
                });
                qZonePlat.SSOSetting(true);
                qZonePlat.share(qZone);
                break;
            case 6://我的圈子
                callBack.shareResultCallBack(3);
                break;
        }
    }

    public interface ShareResultCallback{
        /**
         * @param result
         * 1成功，2失败，3取消，4我的圈子
         */
        void shareResultCallBack(int result);
    }
}
